
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class ScreenPositionTools
{

    public static bool check_is_ui_obj(GameObject obj)
    {
        // 通过是否挂载`Rect Transform`还是`Transform`来检查是否ui对象

        // 检查是否为 null
        if (obj == null)
        {
            Debug.LogWarning($"GameObject[{obj.name}] is null. Returning false.");
            return false;
        }

        // 如果挂载了 RectTransform，说明是 UI 对象
        RectTransform rectTransform = obj.GetComponent<RectTransform>();
        return rectTransform != null;
    }
    
    public static Vector3 conv_screen_pos_to_word_pos(Vector3 screen_pos, bool set_z_to_zero = true)
    {
        // var res = Camera.main.ScreenToWorldPoint(new Vector3(screen_pos.x, screen_pos.y, screen_pos.z));
        var res = Camera.main.ScreenToWorldPoint(new Vector3(screen_pos.x, screen_pos.y, Camera.main.nearClipPlane));
        res = set_z_to_zero ? set_z_position(res) : res;
        return res;
    }

    public static Vector3 set_z_position(Vector3 position, float z = 0f)
    {
        return new Vector3(position.x, position.y, z);
    }
    
    public static Vector3 conv_word_pos_to_screen_pos(Vector3 screen_pos)
    {
        return Camera.main.WorldToScreenPoint(screen_pos);
    }
    
    public static Vector3 get_delta_for_2_pos(Vector3 screen_pos_1, Vector3 screen_pos_2, bool is_screen_pos_or_word_pos = false)
    {
        Vector3 delta;
        if (is_screen_pos_or_word_pos)
        {
            delta = screen_pos_2 - screen_pos_1;
        }
        else
        {
            Vector3 beginWorldPosition = Camera.main.ScreenToWorldPoint(new Vector3(screen_pos_1.x, screen_pos_1.y, Camera.main.nearClipPlane));
            Vector3 endWorldPosition = Camera.main.ScreenToWorldPoint(new Vector3(screen_pos_2.x, screen_pos_2.y, Camera.main.nearClipPlane));

            // Calculate the delta in world coordinates
            delta = endWorldPosition - beginWorldPosition;
        }
        return delta;
    }
}
